﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Linq;

namespace APBR_Patch_Mirror
{
    public class read
    {
        static string ver;
        public static void XML(string path, int i)
        {
            ArrayList xml = new ArrayList();
            XmlTextReader reader = new XmlTextReader(path);
            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element:
                        xml.Add(Environment.NewLine + "<" + reader.Name);
                        while (reader.MoveToNextAttribute())
                            xml.Add(" " + reader.Name + "='" + reader.Value + "' ");
                        xml.Add("/>");
                        break;
                    case XmlNodeType.Text:
                        xml.Add(reader.Value);
                        break;
                    case XmlNodeType.EndElement:
                        xml.Add(Environment.NewLine + "</" + reader.Name + ">");
                        break;
                }
            }
            ArrayList range = xml.GetRange(i, 1);
            foreach (string value in range)
            {
                ver = value;
            }            
            Info.LocalC_Ver = ver.Split('=', '\'')[2];
            reader.Close();
        }

        public static void XML2(string path, int i)
        {
            ArrayList xml = new ArrayList();
            XmlTextReader reader = new XmlTextReader(path);
            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element:
                        xml.Add(Environment.NewLine + "<" + reader.Name);
                        while (reader.MoveToNextAttribute())
                            xml.Add(" " + reader.Name + "='" + reader.Value + "' ");
                        xml.Add("/>");
                        break;
                    case XmlNodeType.Text:
                        xml.Add(reader.Value);
                        break;
                    case XmlNodeType.EndElement:
                        xml.Add(Environment.NewLine + "</" + reader.Name + ">");
                        break;
                }
            }
            ArrayList range = xml.GetRange(i, 1);
            foreach (string value in range)
            {
                ver = value;
            }
            Info.LocalL_Ver = ver.Split('=', '\'')[2];
            reader.Close();
        }

        public class FileDefinition
        {
            public string FileName { get; set; }
            public long Size { get; set; }
            public long Crc { get; set; }
            public long DownloadSize { get; set; }
            public string Md5 { get; set; }
        }

        public class LauncherSyncer
        {
            public List<FileDefinition> _LauncherFiles;
            public LauncherSyncer(string fileDefinitionPath)
            {
                var doc = XElement.Load(fileDefinitionPath);
                _LauncherFiles = doc.Descendants("File").Select(el =>
                    new FileDefinition
                    {
                        FileName = el.Attribute("Filename").Value,
                        Size = long.Parse(el.Attribute("Size").Value),
                        Crc = long.Parse(el.Attribute("CRC").Value),
                    }
                    ).ToList();
            }
        }

        public class ClientSyncer
        {
            public List<FileDefinition> _ClientFiles;
            public ClientSyncer(string fileDefinitionPath)
            {
                var doc = XElement.Load(fileDefinitionPath);
                _ClientFiles = doc.Descendants("File").Select(el =>
                    new FileDefinition
                    {
                        FileName = el.Attribute("Filename").Value,
                        Size = long.Parse(el.Attribute("Size").Value),
                        Crc = long.Parse(el.Attribute("CRC").Value),
                    }
                    ).ToList();
            }
        }
    }
}
